package revontuli.aMHD;

import java.util.ArrayList;

import revontuli.aMHD.R;
import revontuli.aMHD.UIhelpers.StopScheduleListAdapter;
import revontuli.aMHD.dataclasses.Departure;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

public class StopScheduleAct extends Activity {
	private ListView mListView;
	private StopScheduleListAdapter mListAdapter;
	private int stopID;
	
	UserLocationSvc mService;
	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			UserLocationSvc.ClientBinder binder = (UserLocationSvc.ClientBinder) service;
			mService = binder.getService();
			mService.registerMessenger(null);
			
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		stopID = getIntent().getIntExtra("stopID", 0);
		
		//nacitanie rozlozenia
		setContentView(R.layout.stopschedule);
		
		mListAdapter = new StopScheduleListAdapter(this);
		mListView = (ListView)findViewById(R.id.stopScheduleListView);
		mListView.setAdapter(mListAdapter);
		
		//vytvorenie zoznamu odchodov
		ArrayList<Departure> mDepartures = new ArrayList<Departure>();
		mDepartures = aMHDApp.mDataManager.getStopSchedule(stopID);

		//pridanie zoznamu odchodov do adaptera
		mListAdapter.addDepartures(mDepartures);
		
	}
	
	@Override
	public void onStart() {
		super.onStart();
		bindService(new Intent(this, UserLocationSvc.class), mConnection, Context.BIND_AUTO_CREATE);
		
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if (mService != null) {
			unbindService(mConnection);
		}
	}
	
	//metody menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.stopschedulemenu, menu);
		return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (aMHDApp.mDataManager.getOffline()) { //offline mod
			//6-offline button, 7-online button
			menu.getItem(6).setVisible(false);
			menu.getItem(7).setVisible(true);
			menu.getItem(1).setEnabled(false);
			menu.getItem(2).setEnabled(false);
		} else { //online mod
			menu.getItem(6).setVisible(true);
			menu.getItem(7).setVisible(false);
			menu.getItem(1).setEnabled(true);
			menu.getItem(2).setEnabled(true);
		}
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.MenuSSFinder:
			startActivity(new Intent(getApplicationContext(), StopFinderAct.class));
			return true;
		case R.id.MenuSSMap:
			startActivity(new Intent(getApplicationContext(), StopMapAct.class));
			return true;
		case R.id.MenuSSConnection:
			startActivity(new Intent(getApplicationContext(), ConnFinderAct.class));
			return true;
		case R.id.MenuSSStopList:
			startActivity(new Intent(getApplicationContext(), StopListAct.class));
			return true;
		case R.id.MenuSSLineList:
			startActivity(new Intent(getApplicationContext(), LineListAct.class));
			return true;
		case R.id.MenuSSSettings:
			startActivity(new Intent(getApplicationContext(), SettingsAct.class));
			return true;
		case R.id.MenuSSOffline:
			aMHDApp.mDataManager.setOffline(true);
			return true;
		case R.id.MenuSSOnline:
			aMHDApp.mDataManager.setOffline(false);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}